/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.dao.impl;

import com.qdu.dao.NoteDao;

import com.qdu.entity.Note;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 12068
 */
public class NoteDaoImpl extends BaseDaoImpl implements NoteDao{

    @Override
    public int insert(Note n) {
         return executeUpdate("insert into Note values(?,?,?,?,?,?)", n.getUno(),n.getUname(),n.getNno(),n.getNst(),n.getNtopic(),n.getNnr());
    }

 @Override
    public int delete(int Nno) {
         return executeUpdate("delete Note where Nno=?", Nno);
    }

    @Override
    public Note getNoteByNno(int Nno) {
         Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Note pa = null;

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Note where Nno=?");
            ps.setInt(1, Nno);
            rs = ps.executeQuery();
            while (rs.next()) {
                pa = new Note(rs.getInt(1),rs.getString(2),rs.getInt(3), rs.getString(4), rs.getString(5),rs.getString(6));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return pa;
    }

    @Override
    public List<Note> getNoteByNtopic(String Ntopic) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Note> list = new ArrayList<>();
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Note where Ntopic like ?");
            ps.setString(1, "%" + Ntopic + "%");//把含有Ntopic的值全部查找
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Note(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getString(4),rs.getString(5),rs.getString(6)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }



    
}
